/*
* $Id: ViewPointWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Vector;

import apiary.Apiary;
import apiary.common.FormHelper;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.db.PointStore.PointRecord;
import apiary.db.managers.DBManager;
import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.settings.SettingsRecord.Field;
import apiary.ui.BackScreen;
import apiary.ui.journal.ViewForm;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.ChooseForm.ChooseValue;

/**
 * Wizard of view point
 * @author lexus
 */
public class ViewPointWizard extends Wizard
{
    /**
     * Constructor
     */
    public ViewPointWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ViewPointWizard(BackScreen parentForm) {
        super(parentForm);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * @deprecated In this wizard don't use
     */
    public void save() {
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        ViewForm pointForm = (ViewForm) nextForm;
        ChooseValue value = (ChooseValue) formData.get(new Integer(1));
        DBManager dBManager = DBManager.getInstance();
        PointRecord point = dBManager.getPoint(value.id);
        if(point == null)
            return;
        Vector fieldsValue = dBManager.getFieldsValue(point.idFieldValue);
        setPointValue((ViewForm)nextForm, point);
        setUserField((ViewForm)nextForm, fieldsValue);
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChoosePoint"), idChoose, this, true);
        FormHelper.fillPointList((ChooseForm) firstForm);
        Integer idView = new Integer(2);
        ViewForm form = new ViewForm(apiary.getLocaleString("ViewPoint"), idView, this);
        form.setBackScreen(firstForm);
        formsNext.put(idChoose, form);
    }

    /**
     * Form is filled data family
     * @param idFamily identificator of family
     * @param viewForm form for fill
     */
    private void setPointValue(ViewForm form, PointRecord point) {
        Apiary apiary = Apiary.getInstance();
        form.append(apiary.getLocaleString("NamePoint").concat(": "));
        form.append(point.namePoint.concat("\n"));
        Field field = (Field) SettingStore.getInstance().getData(SettingsRecord.NOTE_POINT);
        if(field.getValue() == Boolean.TRUE) {
            form.append(apiary.getLocaleString("Note").concat(": "));
            form.append(point.notePoint.concat("\n"));
        }
    }

    /**
     * Form is filled user data family
     * @param userFields user data of family
     * @param form form for fill
     */
    private void setUserField(ViewForm form, Vector userFields) {
        for(int i = 0; i < userFields.size(); i++) {
            FieldsValueRecord fvRecord = (FieldsValueRecord) userFields.elementAt(i);
            form.append(fvRecord.nameField.concat(": "));
            form.append(fvRecord.valueField.concat("\n"));
        }
    }
}
